#Main Data - Setup---------------
library(ggplot2)
library(tidyr)
library(xtable)
library(cobalt)
library(stargazer)
rm(list=ls())
df <- read.csv("df_ukr.csv")

#Online Supplement - Table 2---------
df$State<-ifelse(df$UserLanguage=="AR-E","Egypt",NA)
df$State<-ifelse(df$UserLanguage=="AR-SA","Saudi Arabia",df$State)
df$State<-ifelse(df$UserLanguage=="CH-CH","China",df$State)
df$State<-ifelse(df$UserLanguage=="CS","Czech Republic",df$State)
df$State<-ifelse(df$UserLanguage=="CH-T","Taiwan",df$State)
df$State<-ifelse(df$UserLanguage=="DE","Germany",df$State)
df$State<-ifelse(df$UserLanguage=="E-A","Australia",df$State)
df$State<-ifelse(df$UserLanguage=="E-IN" | df$UserLanguage=="HI","India",df$State)
df$State<-ifelse(df$UserLanguage=="E-N","Nigeria",df$State)
df$State<-ifelse(df$UserLanguage=="E-SA","South Africa",df$State)
df$State<-ifelse(df$UserLanguage=="E-US","United States",df$State)
df$State<-ifelse(df$UserLanguage=="EN","United Kingdom",df$State)
df$State<-ifelse(df$UserLanguage=="ES-ES","Mexico",df$State)
df$State<-ifelse(df$UserLanguage=="F-B" | df$UserLanguage=="NL","Belgium",df$State)
df$State<-ifelse(df$UserLanguage=="FR","France",df$State)
df$State<-ifelse(df$UserLanguage=="ID","Indonesia",df$State)
df$State<-ifelse(df$UserLanguage=="IT","Italy",df$State)
df$State<-ifelse(df$UserLanguage=="JA","Japan",df$State)
df$State<-ifelse(df$UserLanguage=="KO","South Korea",df$State)
df$State<-ifelse(df$UserLanguage=="PL","Poland",df$State)
df$State<-ifelse(df$UserLanguage=="PT-BR","Brazil", df$State)
df$State<-ifelse(df$UserLanguage=="SK","Slovakia",df$State)
df$State<-ifelse(df$UserLanguage=="SV","Sweden",df$State)
df$State<-ifelse(df$UserLanguage=="TR","Turkey",df$State)

xtable(table(df$State)) #State-by-State Values
nrow(df) #Total

#Main Paper - Figure 1 -------
df$Ukraine
df$Ukraine_Binary<-ifelse(df$Ukraine>3,1,0)
df$Ukraine_Binary_Less<-ifelse(df$Ukraine<3,1,0)
df$Ukraine_Binary_Neither<-ifelse(df$Ukraine==3,1,0)

df2<-subset(df,df$State!="United States")
#Data
rm(d)
d<-matrix(data=NA, nrow=23, ncol=3); d
for(i in 1:23){
  d[i,1] <- mean(df2$Ukraine_Binary[df2$State==unique(df2$State)[i]])  
  d[i,3] <- mean(df2$Ukraine_Binary_Less[df2$State==unique(df2$State)[i]])  
  d[i,2] <- mean(df2$Ukraine_Binary_Neither[df2$State==unique(df2$State)[i]])  
}
colnames(d) <- c("More","Neither More Nor Less","Less")
d<-as.data.frame(d)
d$State <- unique(df2$State)
d


d <- d %>% pivot_longer(cols=c('More', 'Neither More Nor Less','Less'),
                        names_to='Attitude',
                        values_to='Value')

d$Attitude <- factor(d$Attitude, levels=c('More', 'Neither More Nor Less', 'Less'))

d$Location<-NA
for(i in 1:69){
  d$Location[i] <- ifelse(d$Attitude[i]=='Less', d$Value[i] - 0.02, d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Neither More Nor Less',d$Value[i] + d$Value[d$State==d$State[i]][3] - 0.02 ,d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='More', d$Value[i] + d$Value[d$State==d$State[i]][3] + d$Value[d$State==d$State[i]][2] - 0.02,
                          d$Location[i])
}
d$Location
d[,4]<-d[,4]*100
d[,3]<-d[,3]*100
d

#Graph 

ggplot(d,) + 
  ggtitle("Does the U.S. response to the Russia-Ukraine war make you trust the United States more or less as an ally/partner for your country?") +
  geom_col(aes(y=Value, x=State, fill=Attitude)) + 
  scale_fill_manual(name = "",
                    values = c("More"="skyblue1",
                               "Less"="chocolate3",
                               "Neither More Nor Less"="lightgrey"), 
                    labels = c('More'='More','Neither More Nor Less'= 'Neither More Nor Less', 'Less'='Less')) + 
  geom_text(aes(label = round(Value,2), x=State,y=Location)) + 
  xlab("")+
  ylab("")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45, hjust=1,size=14),
        axis.text.y = element_text(angle = 90, vjust=0.5, hjust=0,size=14),
        plot.title = element_text(size=16),
        legend.text = element_text(size = 14))


#Main Paper - Table 1------------
unique(df$State)
df$AllyStatus<-NA
df$AllyStatus<-ifelse(df$State=="Japan","Ally",NA)
df$AllyStatus<-ifelse(df$State=="India","Partner",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Mexico","Close Partner",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="United Kingdom","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="South Africa","Partner",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="South Korea","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Taiwan","Close Partner",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="France","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Indonesia","Partner",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Italy","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Germany","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Turkey","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Poland","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Brazil","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Australia","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Egypt","Partner",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Belgium","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Nigeria","Partner",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Saudi Arabia","Close Partner",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Sweden","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Czech Republic","Ally",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="United States","USA",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="China","Enemy",df$AllyStatus)
df$AllyStatus<-ifelse(df$State=="Slovakia","Ally",df$AllyStatus)

unique(df$State)
df$Region2<-NA
df$Region2<-ifelse(df$State=="Japan","East Asia",NA)
df$Region2<-ifelse(df$State=="India","South Asia",df$Region2)
df$Region2<-ifelse(df$State=="Mexico","Americas",df$Region2)
df$Region2<-ifelse(df$State=="United Kingdom","Western Europe",df$Region2)
df$Region2<-ifelse(df$State=="South Africa","Africa",df$Region2)
df$Region2<-ifelse(df$State=="South Korea","East Asia",df$Region2)
df$Region2<-ifelse(df$State=="Taiwan","East Asia",df$Region2)
df$Region2<-ifelse(df$State=="France","Western Europe",df$Region2)
df$Region2<-ifelse(df$State=="Indonesia","Southeast Asia",df$Region2)
df$Region2<-ifelse(df$State=="Italy","Southern Europe",df$Region2)
df$Region2<-ifelse(df$State=="Germany","Central Europe",df$Region2)
df$Region2<-ifelse(df$State=="Turkey","Southern Europe",df$Region2)
df$Region2<-ifelse(df$State=="Poland","Eastern Europe",df$Region2)
df$Region2<-ifelse(df$State=="Brazil","Americas",df$Region2)
df$Region2<-ifelse(df$State=="Australia","East Asia",df$Region2)
df$Region2<-ifelse(df$State=="Egypt","Middle East",df$Region2)
df$Region2<-ifelse(df$State=="Belgium","Western Europe",df$Region2)
df$Region2<-ifelse(df$State=="Nigeria","Africa",df$Region2)
df$Region2<-ifelse(df$State=="Saudi Arabia","Middle East",df$Region2)
df$Region2<-ifelse(df$State=="Sweden","Western Europe",df$Region2)
df$Region2<-ifelse(df$State=="Czech Republic","Central Europe",df$Region2)
df$Region2<-ifelse(df$State=="United States","Americas",df$Region2)
df$Region2<-ifelse(df$State=="China","East Asia",df$Region2)
df$Region2<-ifelse(df$State=="Slovakia","Eastern Europe",df$Region2)

unique(df$State)
df$Vulnerability<-NA
df$Vulnerability<-ifelse(df$State=="Japan","Moderate",NA)
df$Vulnerability<-ifelse(df$State=="India","Low",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Mexico","Low",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="United Kingdom","High",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="South Africa","Low",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="South Korea","Moderate",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Taiwan","Moderate",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="France","High",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Indonesia","Low",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Italy","High",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Germany","High",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Turkey","High",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Poland","Frontline",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Brazil","Low",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Australia","Moderate",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Egypt","Low",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Belgium","High",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Nigeria","Low",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Saudi Arabia","Low",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Sweden","Frontline",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Czech Republic","High",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="United States","High",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="China","Moderate",df$Vulnerability)
df$Vulnerability<-ifelse(df$State=="Slovakia","Frontline",df$Vulnerability)

df$RegimeType<-NA #Polity IV Score (2018)
df$RegimeType<-ifelse(df$State=="Japan","10",NA)
df$RegimeType<-ifelse(df$State=="India","9",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Mexico","8",df$RegimeType)
df$RegimeType<-ifelse(df$State=="United Kingdom","8",df$RegimeType)
df$RegimeType<-ifelse(df$State=="South Africa","9",df$RegimeType)
df$RegimeType<-ifelse(df$State=="South Korea","8",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Taiwan","10",df$RegimeType)
df$RegimeType<-ifelse(df$State=="France","9",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Indonesia","9",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Italy","10",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Germany","10",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Turkey","-4",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Poland","10",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Brazil","8",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Australia","10",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Egypt","-4",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Belgium","8",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Nigeria","7",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Saudi Arabia","-10",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Sweden","10",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Czech Republic","9",df$RegimeType)
df$RegimeType<-ifelse(df$State=="United States","8",df$RegimeType)
df$RegimeType<-ifelse(df$State=="China","-7",df$RegimeType)
df$RegimeType<-ifelse(df$State=="Slovakia","10",df$RegimeType)
df$RegimeType<-as.numeric(df$RegimeType)


df$WhyTrustUS_1 <- ifelse(is.na(df$WhyTrustUS_1),0,1) #Economic & Diplomatic
df$WhyTrustUS_4 <- ifelse(is.na(df$WhyTrustUS_4),0,1) #Did Not Send Forces
df$WhyTrustUS_5 <- ifelse(is.na(df$WhyTrustUS_5),0,1) #Supported Ukraine
df$WhyTrustUS_6 <- ifelse(is.na(df$WhyTrustUS_6),0,1) #Supported NATO
df$WhyTrustUS_7 <- ifelse(is.na(df$WhyTrustUS_7),0,1) #Sufficiently Cautious
df$WhyTrustUS_8 <- ifelse(is.na(df$WhyTrustUS_8),0,1) #Other

df$WhyDistrustUS_1 <- ifelse(is.na(df$WhyDistrustUS_1),0,1) #Should Have Used Econ/Dip
df$WhyDistrustUS_4 <- ifelse(is.na(df$WhyDistrustUS_4),0,1) #Should Have Sent Armed Forces
df$WhyDistrustUS_5 <- ifelse(is.na(df$WhyDistrustUS_5),0,1) #Should Have Supported Ukraine More
df$WhyDistrustUS_6 <- ifelse(is.na(df$WhyDistrustUS_6),0,1) #Should Have Supported NATO More
df$WhyDistrustUS_7 <- ifelse(is.na(df$WhyDistrustUS_7),0,1) #US Is Responsible
df$WhyDistrustUS_8 <- ifelse(is.na(df$WhyDistrustUS_8),0,1) #Other

df$Restraint<-df$WhyTrustUS_1 + df$WhyTrustUS_4 + df$WhyTrustUS_7 + df$WhyDistrustUS_1 
df$StrongRestraint<-df$WhyTrustUS_4 + df$WhyTrustUS_7 + df$WhyDistrustUS_1 
df$Resolve<-df$WhyTrustUS_5 + df$WhyTrustUS_6 + df$WhyDistrustUS_4 + df$WhyDistrustUS_5 + df$WhyDistrustUS_6

df$RestraintAndResolve<-ifelse(df$StrongRestraint>0 & df$Resolve>0 & df$Ukraine > 2, 1, 0)
sum(df$RestraintAndResolve) / nrow(subset(df,df$Ukraine>2))

df$RestraintAndResolve2<-ifelse(df$StrongRestraint>0 & df$Resolve>0 & df$Ukraine < 4, 1, 0)
sum(df$RestraintAndResolve2) / nrow(subset(df,df$Ukraine<4))

df$Female<-ifelse(df$Gender==1,1,0)
df$Inc<-ifelse(df$Income<99,df$Income,NA)
df$Education<-ifelse(df$Edu<99,df$Edu,NA)

#Reassured by Restraint

mod4A<-lm(data=df[df$Ukraine > 2,], StrongRestraint ~ FavorRussia + FavorUkraine + FavorUS + 
           Dove + Vulnerability + RegimeType + AllyStatus + 
           Age + Female + MilExperience + Education + Inc)
summary(mod4A)

mod4AE<-lm(data=df[df$Ukraine > 2 & df$PolicyElite==1,], StrongRestraint ~ FavorRussia + FavorUkraine + FavorUS + 
             Dove + Vulnerability + RegimeType + AllyStatus + 
             Age + Female + MilExperience + Education + Inc)
summary(mod4AE)

#Reassured by Resolve
mod4B<-lm(data=df[df$Ukraine > 2,], Resolve ~ FavorRussia + FavorUkraine + FavorUS + 
            Dove + Vulnerability + RegimeType + AllyStatus + 
            Age + Female + MilExperience + Education + Inc)
summary(mod4B)

mod4BE<-lm(data=df[df$Ukraine > 2 & df$PolicyElite==1,], Resolve ~ FavorRussia + FavorUkraine + FavorUS + 
             Dove + Vulnerability + RegimeType + AllyStatus + 
             Age + Female + MilExperience + Education + Inc)
summary(mod4BE)

stargazer(mod4A,mod4AE,mod4B,mod4BE,single.row=T)

#Online Supplement - Table 3--------------
#Not Reassured -- Lack of Restraint
mod5A<-lm(data=df[df$Ukraine < 4,], StrongRestraint ~ FavorRussia + FavorUkraine + FavorUS + 
            Dove + Vulnerability + RegimeType + AllyStatus + 
            Age + Female + MilExperience + Education + Inc)
summary(mod5A)

mod5AE<-lm(data=df[df$Ukraine < 4 & df$PolicyElite==1,], StrongRestraint ~ FavorRussia + FavorUkraine + FavorUS + 
             Dove + Vulnerability + RegimeType + AllyStatus + 
             Age + Female + MilExperience + Education + Inc)
summary(mod5AE)

#Not Reassured -- Lack of Resolve
mod5B<-lm(data=df[df$Ukraine < 4,], Resolve ~ FavorRussia + FavorUkraine + FavorUS + 
            Dove + Vulnerability + RegimeType + AllyStatus + 
            Age + Female + MilExperience + Education + Inc)
summary(mod5B)

mod5BE<-lm(data=df[df$Ukraine < 4 & df$PolicyElite==1,], Resolve ~ FavorRussia + FavorUkraine + FavorUS + 
             Dove + Vulnerability + RegimeType + AllyStatus + 
             Age + Female + MilExperience + Education + Inc)
summary(mod5BE)

stargazer(mod5A,mod5AE,mod5B,mod5BE,single.row=T)

#Online Supplement - Table 4--------------
#Reassured by Restraint - Interactions

mod6A<-lm(data=df[df$Ukraine > 2,], StrongRestraint ~ FavorRussia + FavorUkraine + FavorUS + 
            Vulnerability + RegimeType + AllyStatus * Dove + 
            Age + Female + MilExperience + Education + Inc)
summary(mod6A)

mod6AE<-lm(data=df[df$Ukraine > 2 & df$PolicyElite==1,], StrongRestraint ~ FavorRussia + FavorUkraine + FavorUS + 
            Vulnerability + RegimeType + AllyStatus * Dove + 
             Age + Female + MilExperience + Education + Inc)
summary(mod6AE)

#Reassured by Resolve - Interactions 
mod6B<-lm(data=df[df$Ukraine > 2,], Resolve ~ FavorRussia + FavorUkraine + FavorUS + 
            Vulnerability + RegimeType + AllyStatus * Dove + 
            Age + Female + MilExperience + Education + Inc)
summary(mod6B)

mod6BE<-lm(data=df[df$Ukraine > 2 & df$PolicyElite==1,], Resolve ~ FavorRussia + FavorUkraine + FavorUS + 
             Vulnerability + RegimeType + AllyStatus * Dove + 
             Age + Female + MilExperience + Education + Inc)
summary(mod6BE)

stargazer(mod6A,mod6AE,mod6B,mod6BE,single.row=T)


#Online Supplement - Figure 3------------
rm(d)
d<-matrix(data=NA, nrow=24, ncol=10); d
for(i in 1:24){
  d[i,1] <- mean(df$WhyTrustUS_1[df$State==unique(df$State)[i]]) 
  d[i,2] <- mean(df$WhyTrustUS_4[df$State==unique(df$State)[i]]) 
  d[i,3] <- mean(df$WhyTrustUS_5[df$State==unique(df$State)[i]]) 
  d[i,4] <- mean(df$WhyTrustUS_6[df$State==unique(df$State)[i]]) 
  d[i,5] <- mean(df$WhyTrustUS_7[df$State==unique(df$State)[i]]) 
  
  d[i,6] <- mean(df$WhyDistrustUS_1[df$State==unique(df$State)[i]]) 
  d[i,7] <- mean(df$WhyDistrustUS_4[df$State==unique(df$State)[i]]) 
  d[i,8] <- mean(df$WhyDistrustUS_5[df$State==unique(df$State)[i]]) 
  d[i,9] <- mean(df$WhyDistrustUS_6[df$State==unique(df$State)[i]]) 
  d[i,10] <- mean(df$WhyDistrustUS_7[df$State==unique(df$State)[i]]) 
  
  
}
colnames(d) <- c("Approve economic and diplomatic measures","Approve not sending forces", "Approve supporting Ukranian military",
                 "Approve support to NATO","Approve exercise of caution",
                 "Disapprove supporting Ukrainian military","Disapprove of failure to send forces","Disapprove insufficient support for Ukrainian military",
                 "Disapprove insufficient support for NATO","U.S. responsible for war")
d<-as.data.frame(d)
d$State <- unique(df$State)
d[,1:10]<-d[,1:10]*100;head(d)

library(tidyr)
d <- d %>% pivot_longer(cols=c("Approve economic and diplomatic measures","Approve not sending forces", "Approve supporting Ukranian military",
                               "Approve support to NATO","Approve exercise of caution",
                               "Disapprove supporting Ukrainian military","Disapprove of failure to send forces","Disapprove insufficient support for Ukrainian military",
                               "Disapprove insufficient support for NATO","U.S. responsible for war"),
                        names_to='Statement',
                        values_to='Value')
d <- as.data.frame(d)

p1 <- ggplot(d[d$State=="Australia",],aes(x=Statement)) + 
  ggtitle("Australia") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p2 <- ggplot(d[d$State=="Belgium",],aes(x=Statement)) + 
  ggtitle("Belgium") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p3 <- ggplot(d[d$State=="Brazil",],aes(x=Statement)) + 
  ggtitle("Brazil") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p4 <- ggplot(d[d$State=="China",],aes(x=Statement)) + 
  ggtitle("China") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p5 <- ggplot(d[d$State=="Czech Republic",],aes(x=Statement)) + 
  ggtitle("Czech Republic") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p6 <- ggplot(d[d$State=="Egypt",],aes(x=Statement)) + 
  ggtitle("Egypt") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p7 <- ggplot(d[d$State=="France",],aes(x=Statement)) + 
  ggtitle("France") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p8 <- ggplot(d[d$State=="Germany",],aes(x=Statement)) + 
  ggtitle("Germany") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p9 <- ggplot(d[d$State=="India",],aes(x=Statement)) + 
  ggtitle("India") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p10 <- ggplot(d[d$State=="Indonesia",],aes(x=Statement)) + 
  ggtitle("Indonesia") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p11 <- ggplot(d[d$State=="Italy",],aes(x=Statement)) + 
  ggtitle("Italy") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p12 <- ggplot(d[d$State=="Japan",],aes(x=Statement)) + 
  ggtitle("Japan") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p13 <- ggplot(d[d$State=="Mexico",],aes(x=Statement)) + 
  ggtitle("Mexico") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p14 <- ggplot(d[d$State=="Nigeria",],aes(x=Statement)) + 
  ggtitle("Nigeria") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p15 <- ggplot(d[d$State=="Poland",],aes(x=Statement)) + 
  ggtitle("Poland") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p16 <- ggplot(d[d$State=="Saudi Arabia",],aes(x=Statement)) + 
  ggtitle("Saudi Arabia") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p17 <- ggplot(d[d$State=="Slovakia",],aes(x=Statement)) + 
  ggtitle("Slovakia") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p18 <- ggplot(d[d$State=="South Africa",],aes(x=Statement)) + 
  ggtitle("South Africa") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p19 <- ggplot(d[d$State=="South Korea",],aes(x=Statement)) + 
  ggtitle("South Korea") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p20 <- ggplot(d[d$State=="Sweden",],aes(x=Statement)) + 
  ggtitle("Sweden") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p21 <- ggplot(d[d$State=="Taiwan",],aes(x=Statement)) + 
  ggtitle("Taiwan") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p22 <- ggplot(d[d$State=="Turkey",],aes(x=Statement)) + 
  ggtitle("Turkey") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p23 <- ggplot(d[d$State=="United Kingdom",],aes(x=Statement)) + 
  ggtitle("United Kingdom") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "none") +
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())

p23_legend <- ggplot(d[d$State=="United Kingdom",],aes(x=Statement)) + 
  ggtitle("United Kingdom") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  theme(legend.position = "right") + 
  ylim(0,60)+
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank());p23_legend

library(cowplot)
legend<-get_legend(p23_legend);legend


library(gridExtra)
library(grid)
library(ggplot2)
library(lattice) 
library(ggpubr)
fullplot <- ggarrange(p1, p2, p3, p4, p5, p6, p7, p8,
                      p9, p10, p11, p12, p13, p14, p15, p16,
                      p17, p18, p19, p20, p21, p22, p23 + theme(legend.position="none"),
                      legend, nrow = 4, ncol=6)
annotate_figure(fullplot, 
                top = text_grob("Respondents' Reasons for (Dis)Trusting U.S.", 
                                color = "black", face = "bold", size = 18))




#Main Paper - Figure 2------------
d<-matrix(data=NA, nrow=1, ncol=10);d
  d[1,1] <- mean(df$WhyTrustUS_1) 
  d[1,2] <- mean(df$WhyTrustUS_4) 
  d[1,3] <- mean(df$WhyTrustUS_5) 
  d[1,4] <- mean(df$WhyTrustUS_6) 
  d[1,5] <- mean(df$WhyTrustUS_7) 
  
  d[1,6] <- mean(df$WhyDistrustUS_1)  
  d[1,7] <- mean(df$WhyDistrustUS_4) 
  d[1,8] <- mean(df$WhyDistrustUS_5) 
  d[1,9] <- mean(df$WhyDistrustUS_6)  
  d[1,10] <- mean(df$WhyDistrustUS_7) 

colnames(d) <- c("Approve economic and diplomatic measures","Approve not sending forces", "Approve supporting Ukranian military",
                 "Approve support to NATO","Approve exercise of caution",
                 "Disapprove supporting Ukrainian military","Disapprove of failure to send forces","Disapprove insufficient support for Ukrainian military",
                 "Disapprove insufficient support for NATO","U.S. responsible for war")
d<-as.data.frame(d)
d[,1:10]<-d[,1:10]*100;d

library(tidyr)
d <- d %>% pivot_longer(cols=c("Approve economic and diplomatic measures","Approve not sending forces", "Approve supporting Ukranian military",
                               "Approve support to NATO","Approve exercise of caution",
                               "Disapprove supporting Ukrainian military","Disapprove of failure to send forces","Disapprove insufficient support for Ukrainian military",
                               "Disapprove insufficient support for NATO","U.S. responsible for war"),
                        names_to='Statement',
                        values_to='Value')
d <- as.data.frame(d)
d$Value <- round(d$Value,1)

p0 <- ggplot(d,aes(x=Statement,y=Value)) + 
  ggtitle("Respondents' Reasons for (Dis)Trusting the United States") + 
  geom_col(aes(y=Value,fill=Statement)) + 
  ylab("Percent of Respondents Selecting Each Statement") +
  ylim(0,40) +
  geom_text(aes(label = Value), vjust=-1) + 
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_blank(),
        axis.title.x=element_blank()); p0


#Online Supplement - Figure 4------------
subd<-df[,c(16:20,29:33)]; names(subd)
colnames(subd)<-c("Non-Military","Approve Non-Intervention","Aid to Ukraine",
                  "Aid to NATO","Caution","Disapprove Non-Intervention",
                  "Insufficient Aid to Ukraine","Insufficient Aid to NATO",
                  "U.S. Responsible")

correlation_matrix <- cor(subd, use = "pairwise.complete.obs",method="pearson")

library(corrplot)
corrplot(correlation_matrix, method = "color", type="upper",tl.col = 'black')
title(main="Pearson Correlations Between Explanations for (Dis)Trust")



#CEE Data - Setup---------------
rm(list=ls())

df<-read.csv("cee_pretest.csv")
df<-df[df$Consent==1,] 

a <- table(df$UkraineTrust,df$State)
b <- c(sum(a[,1]), sum(a[,2]), sum(a[,3]), sum(a[,4]), sum(a[,5]))
a[,1] <- a[,1]/b[1]
a[,2] <- a[,2]/b[2]
a[,3] <- a[,3]/b[3]
a[,4] <- a[,4]/b[4]
a[,5] <- a[,5]/b[5]
xtable(round(a,4)*100)